package cn.luoshaocheng.basic.controller;

import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.service.IVerifyCodeService;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

/*
* 图形验证码接口
* */
@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;

    @GetMapping("/image/{key}")
    public jsonResult getVerifyCode(@PathVariable(value = "key") String key){
        try {
            String baseStr=iVerifyCodeService.getVerifyCode(key);
            return jsonResult.me().setResultObj(baseStr);
        } catch (BusinessException e) {
            e.printStackTrace();
            return jsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("获取图形验证码失败，请稍后重试");
        }
    }

    @PostMapping("/smsCode")
    public jsonResult smsCode(@RequestBody Map<String,String> map){
        try {
            iVerifyCodeService.smsCode(map);
            return jsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return jsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("系统异常！请稍后再试");
        }
    }
    @PostMapping("/binderSmsCode")
    public jsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            iVerifyCodeService.binderSmsCode(map);
            return jsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return jsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("系统异常！请稍后再试");
        }
    }

}
